package thread;

//懒汉模式，需要几个就创建几个，不着急，最开始不初始化实例，用到才初始化，
class Singleton2{
    private static Singleton2 instence = null;
    private Singleton2(){

    }
    public static Singleton2 getInstance(){
        Object locker = new Object();//也可这个加锁
        if(instence == null) {   //判断是否需要加锁
            synchronized (Singleton.class) {   //涉及读和修改操作，需要加锁才线程安全  类对象（只有一个）
                if (instence == null) {   //判断是否要修改
                    instence = new Singleton2();
                }
            }
        }
        return instence;
    }
}
public class Demo10 {
    public static void main(String[] args) {
        Singleton2 singleton2 = Singleton2.getInstance();
    }
}
